// 订单支付 api
async function payOrder(code,first=true,orderId){
	const res = await uni.$request.post('/order/payOrder', {'code':code});
	console.log('payOrder res: ', res);
	if(res.code == 200){
		// 创建订单成功，则走支付通道，目前只微信支付
		wxPay(res.data,first,orderId);
		uni.$emit('cartUpdate');
	}else{
		uni.showModal({
			title: '提示',
			content: res.msg || '订单生成失败',
			showCancel: false
		});
	}
}
// 直接微信支付
function wxPay(data,first,orderId){
	console.log('orderInfo: ',JSON.parse(data.orderInfo));
	console.log('first,orderId: ',first,orderId);
	let orderInfo = JSON.parse(data.orderInfo)
	if(!orderInfo){
		uni.showModal({
			title: '提示',
			content: 'orderInfo订单信息获取失败',
			showCancel: false
		});
		return;
	}
	uni.requestPayment({
		provider: 'wxpay',
		timeStamp: orderInfo.timeStamp,
		nonceStr: orderInfo.nonceStr,
		package: orderInfo.packageVal,
		signType: 'MD5',
		paySign: orderInfo.paySign,
		success: function (res) {
			console.log('success:' + JSON.stringify(res));
			uni.redirectTo({
				url: '/pages/orders/paySuccess?orderId='+orderId
			});
			// uni.showToast({
			// 	title: '下单成功',
			// 	icon:'none'
			// });
			// setTimeout(function() {
			// 	uni.redirectTo({
			// 		url: '/pages/orders/orders?tabIndex=0'
			// 	});
			// }, 1200);
		},
		fail: function (err) {
			console.log('fail:' + JSON.stringify(err));
			if(first){
				uni.showToast({
					title: '订单未支付，可在我的订单再次支付',
					icon:'none'
				});
				setTimeout(function() {
					uni.navigateBack();
				}, 1200);
			}else{
				uni.showToast({
					title: '支付取消',
					icon:'none'
				});
			}
		}
	});
}
export {
	payOrder
};